using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Pop
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D background;
        Pallet pallete;
        Ball ball;
        Texture2D palleteImg, ballImg, blocoImg;
        SoundEffect soundEffectBall, soundEffectBlock;
        int numBlocksInWindow;
        int numBlocksLines = 6;
        List<Pallet> blocos;
        enum Gamestate { MENU, PAUSE, GAME};
        enum Option { START, EXIT };
        Gamestate gamestate;
        Option option;
        KeyboardState oldstate;
        SpriteFont menufont,debugFont;

        

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            blocos = new List<Pallet>();
            gamestate = Gamestate.MENU;
            option = Option.START;
            base.Initialize();

        }


        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            menufont = Content.Load<SpriteFont>("MenuFont");
            debugFont = Content.Load<SpriteFont>("debugFont");
            soundEffectBall = Content.Load<SoundEffect>("pong1");
            soundEffectBlock = Content.Load<SoundEffect>("block");
            palleteImg = Content.Load<Texture2D>("Arkpallete");
            background = Content.Load<Texture2D>("background");
            blocoImg = Content.Load<Texture2D>("big_block");
            ballImg = Content.Load<Texture2D>("ball");
            pallete = new Pallet(palleteImg, new Vector2((Window.ClientBounds.Width - palleteImg.Width) / 2, Window.ClientBounds.Height - palleteImg.Height));
            ball = new Ball(Window, ballImg, Vector2.Zero, soundEffectBall);

            numBlocksInWindow = (int)((Window.ClientBounds.Width / blocoImg.Width) * 0.8F);


            for (int j = 0; j < numBlocksLines; j++)
            {
                for (int i = 0; i < numBlocksInWindow; i++)
                {
                    Vector2 V2 = new Vector2(i * blocoImg.Width * 1F, 100 + j * blocoImg.Height * 1F);
                    Bloco bTemp = new Bloco(blocoImg, V2, Color.YellowGreen, soundEffectBlock);
                    blocos.Add(bTemp);
                }
            }
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            switch (gamestate)
            {

                case Gamestate.MENU:
                    if ((Keyboard.GetState().IsKeyDown(Keys.Down)) && oldstate.IsKeyUp(Keys.Down) && (option < Option.EXIT))
                        option++;
                    if ((Keyboard.GetState().IsKeyDown(Keys.Up)) && oldstate.IsKeyUp(Keys.Up) && (option > Option.START))
                        option--;
                    if ((Keyboard.GetState().IsKeyDown(Keys.Enter)) && oldstate.IsKeyUp(Keys.Up))
                        switch (option)
                        {
                            case (Option.START):
                                gamestate = Gamestate.GAME;
                                break;
                            case (Option.EXIT):
                                this.Exit();
                                break;
                        }
                    break;

                case Gamestate.PAUSE:
                    if ((Keyboard.GetState().IsKeyDown(Keys.P)) && (oldstate.IsKeyUp(Keys.P)))
                        gamestate = Gamestate.GAME;
                    break;

                case Gamestate.GAME:
                    foreach (Pallet p in blocos)
                    {
                        ball.VerificaColisao3(p);
                        //ball.Colisao(p);
                    }
                    //ball.VerificaColisao2(pallete);
                    ball.VerificaColisao3(pallete);
                    if (ball.Update(Window) == 1)
                    {
                        ball.reset(Window);
                    }
                    pallete.Update(Window,
                    new List<Keys>() { Keys.Left, Keys.Right });

                    if ((Keyboard.GetState().IsKeyDown(Keys.P)) && oldstate.IsKeyUp(Keys.P))
                        gamestate = Gamestate.PAUSE;
                    break;
            }
            oldstate = Keyboard.GetState();
            base.Update(gameTime);

        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.DarkBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            switch (gamestate)
            {

                case Gamestate.MENU:
                    if (option == Option.START)
                    {
                        spriteBatch.DrawString(menufont, "START", new Vector2(60, 60), Color.White);
                        spriteBatch.DrawString(menufont, "EXIT", new Vector2(60, 80), Color.Gray);
                    }
                    else
                    {
                        spriteBatch.DrawString(menufont, "START", new Vector2(60, 60), Color.Gray);
                        spriteBatch.DrawString(menufont, "EXIT", new Vector2(60, 80), Color.White);
                    }

                    break;
                case Gamestate.PAUSE:
                    Drawgame();
                    spriteBatch.DrawString(menufont, "PAUSADO", Vector2.Zero, Color.White);
                    break;
                case Gamestate.GAME:
                    Drawgame();
                    break;
            }
            spriteBatch.End();
            base.Draw(gameTime);
        }

        void Drawgame()
        {
            spriteBatch.Draw(background, Vector2.Zero, Color.White);
            foreach (Pallet p in blocos)
            {
                p.Draw(spriteBatch);
            }
            pallete.Draw(spriteBatch);
            //ball.Draw(spriteBatch);
            ball.Draw(spriteBatch,debugFont);
        }
    }
}
